package goodenglish.domain.service.analysis;

import goodenglish.domain.model.lexicon.NewWord;
import goodenglish.domain.model.profword.ProfWordLibRepository;
import goodenglish.infrastructure.TextParsing;
import goodenglish.infrastructure.repository.db.ProfWordLibRepositoryDb;
import goodenglish.interfaces.BeanFactory;
import goodenglish.interfaces.UUID;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public abstract class ImportAnalyzer extends AbsAnalyzer {
	public ImportAnalyzer(final File file) {
		super(file);
	}

	@Override
	public String analysisSubtName() {

		return file.getName().split("\\.")[0];

	}

	/**
	 * 不包含重复的
	 * 
	 * @param zimuStr
	 * @return
	 */
	@Override
	public List<NewWord> analysisWords() {
		List<NewWord> newWords = new ArrayList<NewWord>();
		Set<String> seplls = new HashSet<String>();
		String wordArray[] = TextParsing.extractWordsFromString(subtStr);

		for (String str : wordArray) {
			String wordSpell = str.trim();
			if (!wordSpell.isEmpty() && !seplls.contains(wordSpell)) {
				if (!profWordsRepository.getProfWordLib().isProf(wordSpell)) {
					seplls.add(wordSpell);
					NewWord newWord = new NewWord();
					newWord.setId(UUID.getUUID());
					newWord.setSpell(wordSpell);
					newWords.add(newWord);
				}

			}

		}
		return newWords;
	}

	private ProfWordLibRepository profWordsRepository = BeanFactory.getSingleInstance(ProfWordLibRepositoryDb.class);
}
